package com.bauer.mq.rabbitmq.config;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @author: weirui
 * @create: 2021-03-05 14:01
 **/
@Component
public class MQInit {

    @Autowired
    private AmqpAdmin amqpAdmin;

    @PostConstruct
    public void init() {
        Map<String, Object> arguments = new HashMap<String, Object>();
        arguments.put("x-dead-letter-exchange", "deadExchange");
        arguments.put("x-message-ttl", 5000);
        Queue queue = new Queue("test.queue",true, false, false,arguments);
        Exchange exchange =  new DirectExchange("testExchange");

        amqpAdmin.declareQueue(queue);
        amqpAdmin.declareExchange(exchange);
        amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("test").noargs());


        Queue deadQueue = new Queue("test.dead.queue");
        Exchange deadExchange =  new DirectExchange("deadExchange");
        amqpAdmin.declareQueue(deadQueue);
        amqpAdmin.declareExchange(deadExchange);
        amqpAdmin.declareBinding(BindingBuilder.bind(deadQueue).to(deadExchange).with("test").noargs());
    }
}
